import CommonUtils from "../btoolkit/CommonUtils";
import BButton from "../btoolkit/components/BButton";
import AudioManager from "../btoolkit/AudioManager";
import SDKManager, { Platform } from "../btoolkit/sdk/SDKManager";
import TouTiaoRecorder from "../btoolkit/sdk/toutiao/TouTiaoRecorder";
import App from "../btoolkit/framework/App";
import QuLe from "../btoolkit/sdk/QuLe";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PopupShare extends cc.Component {

    @property(cc.Node)
    bg: cc.Node = null;

    @property(cc.Sprite)
    ad: cc.Sprite = null;

    @property(cc.Node)
    btnShare: cc.Node = null;

    @property(cc.Node)
    btnClose: cc.Node = null;

    @property({ type: [cc.SpriteFrame] })
    frames: cc.SpriteFrame[] = [];

    private static instance: PopupShare = null;
    private onFinish: Function;
    private shareVideoPath: string;
    private dialogFrameName: string;
    private canPlayVoice = true;
    static shareImgIndex = 0;

    /**
     * 弹出开始分享面板，选择回到主页，或立即分享
     * @param onFinish 成功返回true,否则false
     */
    static show(onFinish: Function, shareVideoPath?: string, dialogFrameName?: string, canPlayVoice = true) {
        if (PopupShare.instance) {
            console.error('不能重复创建PopupShare');
            return;
        }
        PopupShare.instance = cc.instantiate(App.instance.resPreloader.getRes('prefabs/PopupShare')).getComponent(PopupShare);
        PopupShare.instance.onFinish = onFinish;
        PopupShare.instance.shareVideoPath = shareVideoPath;
        PopupShare.instance.dialogFrameName = dialogFrameName;
        PopupShare.instance.canPlayVoice = canPlayVoice;
        cc.Canvas.instance.node.addChild(PopupShare.instance.node);
    }

    onDestroy() {
        PopupShare.instance = null;
    }

    onLoad() {
        CommonUtils.setTouchMask(this.node);

        if (this.canPlayVoice) {
            AudioManager.playVoice('common/voice_PleaseShare');
        }

        //背景动画
        this.bg.opacity = 0;
        cc.tween(this.bg).to(0.5, { opacity: 100 }).start();

        this.ad.node.active = false;
        this.ad.node.scale = 0;

        if (this.dialogFrameName) {
            let atlas: cc.SpriteAtlas = App.instance.resPreloader.getRes('textures/common/common');
            this.ad.spriteFrame = atlas.getSpriteFrame(this.dialogFrameName);
            this.ad.sizeMode = cc.Sprite.SizeMode.TRIMMED;
        } else {
            PopupShare.shareImgIndex = CommonUtils.randomInt(0, this.frames.length);
            this.ad.spriteFrame = this.frames[PopupShare.shareImgIndex];
        }

        BButton.addButton(this.btnClose).setTouchUpEvent(() => {
            this.node.destroy();
            if (this.onFinish) {
                this.onFinish(false);
                this.onFinish = null;
            }
        });

        BButton.addButton(this.btnShare).setTouchUpEvent(() => {
            if (SDKManager.platform == Platform.TouTiao && this.shareVideoPath) {
                TouTiaoRecorder.instance.shareVideo(this.shareVideoPath, '', (success: boolean) => {
                    this.node.destroy();
                    if (this.onFinish) {
                        this.onFinish(success);
                        this.onFinish = null;
                    }
                })
            } else {
                SDKManager.instance.share((over3seconds: boolean) => {
                    this.node.destroy();
                    if (this.onFinish) {
                        this.onFinish(over3seconds);
                        this.onFinish = null;
                    }
                });
            }
        });

        this.showAd();
    }

    private showAd() {
        this.ad.node.active = true;
        cc.tween(this.ad.node).to(0.5, { scale: 1 }, { easing: 'backOut' }).start();
    }

}
